import IValidate from '@/model/other/IValidate'

const validate: Record<string, IValidate> = {
  email: {
    emptyErrorMsg: '请输入邮箱',
    errorMsg: '邮箱格式错误',
    validate(value: string) {
      return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)
    },
  },
  password: {
    emptyErrorMsg: '请输入密码',
    errorMsg:
      '密码格式错误：必须包含大小写字母和数字，可以使用特殊字符，长度8-20',
    validate(value: string) {
      return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/.test(value)
    },
  },
  code: {
    emptyErrorMsg: '请输入验证码',
    errorMsg: '验证码格式错误',
    validate(value: string) {
      return /^[A-Za-z0-9]{6}$/.test(value)
    },
  },
  nickName: {
    emptyErrorMsg: '请输入昵称',
    errorMsg: '昵称格式错误：只能包含中文，数字，字母，下划线，长度2-20',
    validate(value: string) {
      return /^[\u4E00-\u9FA5A-Za-z0-9_]{2,20}$/.test(value)
    },
  },
}

const install: Record<
  string,
  (fieldValue: string, values: Record<string, any>) => string
> = {}

Object.keys(validate).forEach((item) => {
  install[item + 'Validate'] = (
    fieldValue: string,
    values: Record<string, any>
  ): string => {
    if (!fieldValue) {
      return validate[item].emptyErrorMsg
    }
    if (!validate[item].validate(fieldValue)) {
      return validate[item].errorMsg
    }
    return ''
  }
})

export default install
